#include "gameselect.h"
#include <QIcon>
#include <QPushButton>
#include <QSound>
#include "widget.h"
#include <QPainter>
#include "gameroom.h"
#include <QTextEdit>
#include <QFile>
#include <QTextStream>

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(1000, 900);
    this->setWindowIcon(QIcon(":/res/icon.jpg"));
    this->setWindowTitle("游戏关卡选择");

    // 返回按钮
    QPushButton* backButton = new QPushButton(this);
    backButton->move(850, 800);
    backButton->setStyleSheet("QPushButton{border : 0px; width : 150px; height : 100px; image : url(:/res/back.png);}"
                              "QPushButton:hover{image : url(:/res/back_2.png);}");
    connect(backButton, &QPushButton::clicked, [=](){
        this->close();
        Widget* gameHall = new Widget;
        gameHall->show();
    });
    connect(backButton, &QPushButton::pressed, [=](){
        QSound::play(":/res/start.wav");
    });

    QFont font("华文行楷", 30);
    // 简单模式按钮
    QPushButton* simpleButton = new QPushButton(this);
    simpleButton->move(400, 100);
    simpleButton->setFont(font);
    simpleButton->setText("简单模式");
    simpleButton->setStyleSheet("QPushButton{border : 5px solid #33ffcc; border-radius: 30px; color : #ee0000}"
                                "QPushButton:hover{border : 5px solid #ee0000; color : #33ffcc}");
    connect(simpleButton, &QPushButton::clicked,[=](){
        this->close();
        GameRoom* gameRoom = new GameRoom;
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        gameRoom->setTimeout(300);
    });
    connect(simpleButton, &QPushButton::pressed,[=](){
        QSound::play(":/res/otto_sprint.wav");
    });

    // 普通模式按钮
    QPushButton* normalButton = new QPushButton(this);
    normalButton->move(400, 300);
    normalButton->setFont(font);
    normalButton->setText("普通模式");
    normalButton->setStyleSheet("QPushButton{border : 5px solid #33ffcc; border-radius: 30px; color : #ee0000}"
                                "QPushButton:hover{border : 5px solid #ee0000; color : #33ffcc}");
    connect(normalButton, &QPushButton::clicked,[=](){
        this->close();
        GameRoom* gameRoom = new GameRoom;
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        gameRoom->setTimeout(200);
    });
    connect(normalButton, &QPushButton::pressed,[=](){
        QSound::play(":/res/otto_sprint.wav");
    });

    // 困难模式
    QPushButton* hardButton = new QPushButton(this);
    hardButton->move(400, 500);
    hardButton->setFont(font);
    hardButton->setText("困难模式");
    hardButton->setStyleSheet("QPushButton{border : 5px solid #33ffcc; border-radius: 30px; color : #ee0000}"
                              "QPushButton:hover{border : 5px solid #ee0000; color : #33ffcc}");
    connect(hardButton, &QPushButton::clicked,[=](){
        this->close();
        GameRoom* gameRoom = new GameRoom;
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        gameRoom->setTimeout(100);
    });
    connect(hardButton, &QPushButton::pressed,[=](){
        QSound::play(":/res/otto_sprint.wav");
    });

    // 最高得分
    QPushButton* topScoreButton = new QPushButton(this);
    topScoreButton->move(400, 700);
    topScoreButton->setFont(font);
    topScoreButton->setText("最高得分");
    topScoreButton->setStyleSheet("QPushButton{border : 5px solid #33ffcc; border-radius: 30px; color : #ee0000}"
                                  "QPushButton:hover{border : 5px solid #ee0000; color : #33ffcc}");
    connect(topScoreButton, &QPushButton::clicked, [=](){
        QWidget* widget = new QWidget;
        widget->setWindowTitle("最高得分");
        widget->setFixedSize(500, 300);

        QTextEdit* edit = new QTextEdit(widget);
        edit->setFont(font);
        edit->setFixedSize(500, 300);

        QFile file("C:/Users/wangd/Desktop/greedy_snake/Snake/res/topScore.txt");
        file.open(QIODevice::ReadOnly);

        QTextStream in(&file);
        int data = in.readLine().toInt();
        file.close();

        edit->append(QString::number(data));
        widget->show();

    });
}

void GameSelect::paintEvent(QPaintEvent *event)
{
    (void)event;

    QPainter painter(this);
    QPixmap pix(":/res/game_select.jpg");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
}
